package data_transfer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;
import java.net.URI;

public class HbaseToHDFS {

    public static String tableName;
    public static String outputPath;

    public static void main(String[] args)
            throws IOException, InterruptedException, ClassNotFoundException {
        tableName = "hello";
        outputPath = "hdfs://localhost:9000/hdfs_text";
        // safety check
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(outputPath), conf);
        if(fs.exists(new Path(outputPath))){
            System.out.println("path already exists, now deleting it ...");
            fs.delete(new Path(outputPath),true);
        }
        //Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        Job job = Job.getInstance(conf);
        job.setJobName("HBaseToHDFS");
        Scan scan = new Scan();
        TableMapReduceUtil.initTableMapperJob(tableName, scan, doMapper.class,
                Text.class, Text.class, job);
        job.setReducerClass(doReducer.class);
        FileOutputFormat.setOutputPath(job, new Path(outputPath));
        MultipleOutputs.addNamedOutput(job, "hdfs", TextOutputFormat.class,
                WritableComparable.class, Writable.class);
        System.exit(job.waitForCompletion(true) ? 0 : 1);


    }

    public static class doMapper extends TableMapper<Text, Text> {
        @Override
        protected void map(ImmutableBytesWritable key, Result value, Mapper<ImmutableBytesWritable, Result, Text, Text>.Context context) throws IOException, InterruptedException {
            Cell[] cells = value.rawCells();
            String rowValue = "";
            for (Cell cell : cells) {
                rowValue = Bytes.toString(CellUtil.cloneValue(cell));
            }
            context.write(new Text("one"), new Text(rowValue));
        }
    }

    public static class doReducer extends Reducer<Text, Text, Text, NullWritable> {
        private MultipleOutputs<Text, NullWritable> mos = null;

        @Override
        protected void setup(Reducer<Text, Text, Text, NullWritable>.Context context)
                throws IOException, InterruptedException {
            mos = new MultipleOutputs<>(context);
        }

        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, NullWritable>.Context context)
                throws IOException, InterruptedException {
            for (Text text : values) {
                mos.write("hdfs", new Text(text), NullWritable.get());
            }
        }

        @Override
        protected void cleanup(Reducer<Text, Text, Text, NullWritable>.Context context)
                throws IOException, InterruptedException {
            mos.close();
        }
    }
}
